﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayWXBCommand : HYNoTranExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			if (LayWXBCommand.hWndRevit == null)
			{
				LayWXBCommand.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			FormLayFireVoltageEquipment formLayFireVoltageEquipment = new FormLayFireVoltageEquipment();
			int num = (int)formLayFireVoltageEquipment.ShowDialog(LayWXBCommand.hWndRevit);
			int wMsg = 7;
			LayWXBCommand.SendMessage(LayWXBCommand.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			if (num == 1)
			{
				this.LayWXBEquipment(formLayFireVoltageEquipment.fireVoltageEquipment);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void LayWXBEquipment(FireVoltageEquipment fireVoltageEquipment)
		{
			try
			{
				HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
				ICollection<ElementId> first = null;
				first = hyequipmentCommon.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_MechanicalEquipment);
				Transaction transaction = new Transaction(base.Doc, "加载族");
				transaction.Start();
				FamilySymbol familySymbol = this.getFamilySymbol(fireVoltageEquipment.Installation);
				transaction.Commit();
				if (familySymbol != null)
				{
					try
					{
						base.UiDoc.PromptForFamilyInstancePlacement(familySymbol);
					}
					catch (Exception)
					{
					}
				}
				Transaction transaction2 = new Transaction(base.Doc, "消防稳压设备布置");
				transaction2.Start();
				ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_MechanicalEquipment);
				new List<ElementId>();
				foreach (ElementId item in first.Intersect(allFamilyInstance))
				{
					allFamilyInstance.Remove(item);
				}
				base.Doc.Regenerate();
				transaction2.Commit();
			}
			catch (Exception)
			{
			}
		}

		private FamilySymbol getFamilySymbol(string installation)
		{
			string text = "";
			if (installation == "立式（甲）")
			{
				return YJKRevitTools.GetFamilySymbolByName("PressureEquipment", "立式增压稳压设备(甲)", base.Doc, ref text);
			}
			if (installation == "卧式（甲）")
			{
				return YJKRevitTools.GetFamilySymbolByName("PressureEquipment", "卧式增压稳压设备(甲)", base.Doc, ref text);
			}
			if (installation == "立式（乙）")
			{
				return YJKRevitTools.GetFamilySymbolByName("PressureEquipment", "立式增压稳压设备(乙)", base.Doc, ref text);
			}
			if (installation == "卧式（乙）")
			{
				return YJKRevitTools.GetFamilySymbolByName("PressureEquipment", "卧式增压稳压设备(乙)", base.Doc, ref text);
			}
			return null;
		}

		private void SetLayedElements(ICollection<ElementId> collectionId1, FireVoltageEquipment fireVoltageEquipment)
		{
			foreach (ElementId elementId in collectionId1)
			{
				Element elementById = base.Doc.GetElementById(elementId);
				if (elementById.GetParameter("产品型号") != null)
				{
					elementById.GetParameter("产品型号").Set(fireVoltageEquipment.Model);
				}
				if (elementById.GetParameter("功率") != null)
				{
					elementById.GetParameter("功率").Set(UnitConvert.CovertToAPI(fireVoltageEquipment.Power * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)39));
				}
				if (elementById.GetParameter("稳压水容积") != null)
				{
					elementById.GetParameter("稳压水容积").Set(UnitConvert.CovertToAPI((double)fireVoltageEquipment.Volume / 1000.0, (Autodesk.Revit.DB.DisplayUnitType)14));
				}
				if (elementById.GetParameter("消防压力P1") != null)
				{
					elementById.GetParameter("消防压力P1").Set(UnitConvert.CovertToAPI(fireVoltageEquipment.Lift * 1000000.0, (Autodesk.Revit.DB.DisplayUnitType)48));
				}
			}
		}

		public static YJKWindowHandle hWndRevit;
	}
}
